const Actions = require('./actions');
const DB = require('app/db.js');

module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },

    onFetch: function (params, cb) {
        let t = this;
        DB.Proj.get_payment_overview(params).then((content) => {// 成功
            content.list.map((item, index) => {
                var situation = ['发起计划', '计划同意', '发起支付', '同意支付', '支付完成'];
                var color = ["#19B663", "#b3b3b3", "#b3b3b3", "#b3b3b3", "#b3b3b3"];
                var progress = ["未完成", "未完成", "未完成", "未完成", "未完成"];
                if (situation.indexOf(item.payPoniterStatus) != -1) {
                    for (let i in situation) {
                        color[i] = "#19B663";
                        progress[i] = "完成";
                        if (item.payPoniterStatus == situation[i]) {
                            break
                        }
                    }
                }
                item.finish_time = item.finish_time instanceof Array ? item.finish_time : [item.finish_time]
                t.data.data = [
                    { color: color[0], description: item.finish_time[0] ? item.finish_time[0] : '', type: "发起计划", progress: progress[0] },
                    { color: color[1], description: item.finish_time[1] ? item.finish_time[1] : '', type: "计划同意", progress: progress[1] },
                    { color: color[2], description: item.finish_time[2] ? item.finish_time[2] : '', type: "发起支付", progress: progress[2] },
                    { color: color[3], description: item.finish_time[3] ? item.finish_time[3] : '', type: "同意支付", progress: progress[3] },
                    { color: color[4], description: item.finish_time[4] ? item.finish_time[4] : '', type: "支付完成", progress: progress[4] },
                ]
            })
            t.updateComponent();
        }).catch((error) => { // 失败 or 有异常被捕获

        });
    },
    onGetProcess: function (params, cb) {
        let t = this;
        // t.data.data = [
        //     { color: "#19B663", type: "合同签订", progress: "已完成",description:'201.8.15' },
        //     { color: "#19B663", type: "方案评审", progress: "已完成",description:'201.8.15-8.30' },
        //     { color: "#19B663", type: "项目实施", progress: "已完成" ,description:'201.8.15-8.30'},
        //     { color: "#19B663", type: "三方测试", progress: "已完成",description:'201.8.15-8.30' },
        //     { color: "#E92C2C", type: "测试报告", progress: "超时",description:'201.8.15-8.30' },
        //     { color: "#B3B3B3", type: "初验", progress: "未完成",description:'201.8.15-8.30' },
        //     { color: "#B3B3B3", type: "试运行", progress: "未完成",description:'201.8.15-8.30' },
        //     { color: "#B3B3B3", type: "终验", progress: "未完成",description:'201.8.15-8.30' },
        // ]
        DB.Proj.get_sigleProj_process({
            process_id: params.process_id
        }).then((content) => {// 成功 
            var nodelists = content.nodelists;
            let array = [];
            for (let i = 0; i < nodelists.length; i++) {
                var node = {};
                node.type = nodelists[i].node_name;
                node.progress = nodelists[i].node_state == 0 ? "已超时" : (nodelists[i].node_state == 1 ? "未完成" : "已完成");
                node.color = nodelists[i].node_state == 0 ? "#E92C2C" : (nodelists[i].node_state == 1 ? "#B3B3B3" : "#19B663");
                let node_array = nodelists[i].node_info;
                node.description = "";
                if (node_array.length > 0) {
                    for (let j = 0; j < node_array.length; j++) {
                        node.description += j == 0 ? node_array[j].value : "-" + node_array[j].value;
                    }
                }
                array.push(node);
            }
            this.data.data = array;
            t.updateComponent();
        }).catch((error) => { // 失败 or 有异常被捕获

        });
    },
    updateComponent: function () {
        this.trigger(this.data);
    },

    getInitialState: function () {
        return this.data;
    }
});
